home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / usr_-_Usr_Files / BIN / POD2LATE.{BN (.txt) < prev    next >
LaTeX Document  |  1999-09-17  |  22KB  |  557 lines

  1. #!/usr/bin/perl
  2.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3.     if $running_under_some_shell;
  4. # pod2latex, version 1.1
  5. # by Taro Kawagish (kawagish@imslab.co.jp),  Jan 11, 1995.
  6. # pod2latex filters Perl pod documents to LaTeX documents.
  7. # What pod2latex does:
  8. # 1. Pod file 'perl_doc_entry.pod' is filtered to 'perl_doc_entry.tex'.
  9. # 2. Indented paragraphs are translated into
  10. #    '\begin{verbatim} ... \end{verbatim}'.
  11. # 3. '=head1 heading' command is translated into '\section{heading}'
  12. # 4. '=head2 heading' command is translated into '\subsection*{heading}'
  13. # 5. '=over N' command is translated into
  14. #        '\begin{itemize}'    if following =item starts with *,
  15. #        '\begin{enumerate}'    if following =item starts with 1.,
  16. #        '\begin{description}'    if else.
  17. #      (indentation level N is ignored.)
  18. # 6. '=item * heading' command is translated into '\item heading',
  19. #    '=item 1. heading' command is translated into '\item heading',
  20. #    '=item heading' command(other) is translated into '\item[heading]'.
  21. # 7. '=back' command is translated into
  22. #        '\end{itemize}'    if started with '\begin{itemize}',
  23. #        '\end{enumerate}'    if started with '\begin{enumerate}',
  24. #        '\end{description}'    if started with '\begin{description}'.
  25. # 8. other paragraphs are translated into strings with TeX special characters
  26. #    escaped.
  27. # 9. In heading text, and other paragraphs, the following translation of pod
  28. #    quotes are done, and then TeX special characters are escaped after that.
  29. #      I<text> to {\em text\/},
  30. #      B<text> to {\bf text},
  31. #      S<text> to text1,
  32. #        where text1 is a string with blank characters replaced with ~,
  33. #      C<text> to {\tt text2},
  34. #        where text2 is a string with TeX special characters escaped to
  35. #        obtain a literal printout,
  36. #      E<text> (HTML escape) to TeX escaped string,
  37. #      L<text> to referencing string as is done by pod2man,
  38. #      F<file> to {\em file\/},
  39. #      Z<> to a null string,
  40. # 10. those headings are indexed:
  41. #       '=head1 heading'   =>  \section{heading}\index{heading}
  42. #       '=head2 heading'   =>  \subsection*{heading}\index{heading}
  43. #                 only when heading does not match frequent patterns such as
  44. #                 DESCRIPTION, DIAGNOSTICS,...
  45. #       '=item heading'   =>  \item{heading}\index{heading}
  46. # Usage:
  47. #     pod2latex perl_doc_entry.pod
  48. # this will write to a file 'perl_doc_entry.tex'.
  49. # To LaTeX:
  50. # The following commands need to be defined in the preamble of the LaTeX
  51. # document:
  52. # \def\C++{{\rm C\kern-.05em\raise.3ex\hbox{\footnotesize ++}}}
  53. # \def\underscore{\leavevmode\kern.04em\vbox{\hrule width 0.4em height 0.3pt}}
  54. # and \parindent should be set zero:
  55. # \setlength{\parindent}{0pt}
  56. # Note:
  57. # This script was written modifing pod2man.
  58. # Bug:
  59. # If HTML escapes E<text> other than E<amp>,E<lt>,E<gt>,E<quot> are used
  60. # in C<>, translation will produce wrong character strings.
  61. # Translation of HTML escapes of various European accents might be wrong.
  62. $/ = "";            # record separator is blank lines
  63. # TeX special characters.
  64. ##$tt_ables = "!@*()-=+|;:'\"`,./?<>";
  65. $backslash_escapables = "#\$%&{}_";
  66. $backslash_escapables2 = "#\$%&{}";    # except _
  67. ##$nonverbables = "^\\~";
  68. ##$bracketesc = "[]";
  69. ##@tex_verb_fences = unpack("aaaaaaaaa","|#@!*+?:;");
  70. @head1_freq_patterns        # =head1 patterns which need not be index'ed
  71.     = ("AUTHOR","Author","BUGS","DATE","DESCRIPTION","DIAGNOSTICS",
  72.        "ENVIRONMENT","EXAMPLES","FILES","INTRODUCTION","NAME","NOTE",
  73.        "SEE ALSO","SYNOPSIS","WARNING");
  74. $indent = 0;
  75. # parse the pods, produce LaTeX.
  76. open(POD,"<$ARGV[0]") || die "cant open $ARGV[0]";
  77. ($pod=$ARGV[0]) =~ s/\.pod$//;
  78. open(LATEX,">$pod.tex");
  79. &do_hdr();
  80. $cutting = 1;
  81. $begun = "";
  82. while (<POD>) {
  83.     if ($cutting) {
  84.     next unless /^=/;
  85.     $cutting = 0;
  86.     }
  87.     if ($begun) {
  88.        if (/^=end\s+$begun/) {
  89.            $begun = "";
  90.        }
  91.        elsif ($begun =~ /^(tex|latex)$/) {
  92.            print LATEX $_;
  93.        }
  94.        next;
  95.     }
  96.     chop;
  97.     length || (print LATEX  "\n") && next;
  98.     # translate indented lines as a verabatim paragraph
  99.     if (/^\s/) {
  100.     @lines = split(/\n/);
  101.     print LATEX  "\\begin{verbatim}\n";
  102.     for (@lines) {
  103.         1 while s
  104.         {^( [^\t]* ) \t ( \t* ) }
  105.         { $1 . ' ' x (8 - (length($1)%8) + 8*(length($2))) }ex;
  106.         print LATEX  $_,"\n";
  107.     print LATEX  "\\end{verbatim}\n";
  108.     next;
  109.     }
  110.     if (/^=for\s+(\S+)\s*/s) {
  111.     if ($1 eq "tex" or $1 eq "latex") {
  112.         print LATEX $',"\n";
  113.     } else {
  114.         # ignore unknown for
  115.     next;
  116.     }
  117.     elsif (/^=begin\s+(\S+)\s*/s) {
  118.     $begun = $1;
  119.     if ($1 eq "tex" or $1 eq "latex") {
  120.         print LATEX $'."\n";
  121.     next;
  122.     }
  123.     # preserve '=item' line with pod quotes as they are.
  124.     if (/^=item/) {
  125.     ($bareitem = $_) =~ s/^=item\s*//;
  126.     }
  127.     # check for things that'll hosed our noremap scheme; affects $_
  128.     &init_noremap();
  129.     # expand strings "func()" as pod quotes.
  130.     if (!/^=item/) {
  131.     # first hide pod escapes.
  132.     # escaped strings are mapped into the ones with the MSB's on.
  133.     s/([A-Z]<[^<>]*>)/noremap($1)/ge;
  134.     # func() is a reference to a perl function
  135.     s{\b([:\w]+\(\))}{I<$1>}g;
  136.     # func(n) is a reference to a man page
  137.     s{(\w+)(\([^\s,\051]+\))}{I<$1>$2}g;
  138.     # convert simple variable references
  139. #    s/([\$\@%][\w:]+)/C<$1>/g;
  140. #    s/\$[\w:]+\[[0-9]+\]/C<$&>/g;
  141.     if (m{ ([\-\w]+\([^\051]*?[\@\$,][^\051]*?\))
  142.            }x && $` !~ /([LCI]<[^<>]*|-)$/ && !/^=\w/)
  143.         warn "``$1'' should be a [LCI]<$1> ref";
  144.     while (/(-[a-zA-Z])\b/g && $` !~ /[\w\-]$/) {
  145.         warn "``$1'' should be [CB]<$1> ref";
  146.     # put back pod quotes so we get the inside of <> processed;
  147.     $_ = &clear_noremap($_);
  148.     }
  149.     # process TeX special characters
  150.     # First hide HTML quotes E<> since they can be included in C<>.
  151.     s/(E<[^<>]+>)/noremap($1)/ge;
  152.     # Then hide C<> type literal quotes.
  153.     # String inside of C<> will later be expanded into {\tt ..} strings
  154.     # with TeX special characters escaped as needed.
  155.     s/(C<[^<>]*>)/&noremap($1)/ge;
  156.     # Next escape TeX special characters including other pod quotes B< >,...
  157.     #
  158.     # NOTE: s/re/&func($str)/e evaluates $str just once in perl5.
  159.     # (in perl4 evaluation takes place twice before getting passed to func().)
  160.     # - hyphen => ---
  161.     s/(\S+)(\s+)-+(\s+)(\S+)/"$1".&noremap(" --- ")."$4"/ge;
  162.     # '-', '--', "-"  =>  '{\tt -}', '{\tt --}', "{\tt -}"
  163. ##    s/("|')(\s*)(-+)(\s*)\1/&noremap("$1$2\{\\tt $3\}$4$1")/ge;
  164. ## changed Wed Jan 25 15:26:39 JST 1995
  165.     # '-', '--', "-"  =>  '$-$', '$--$', "$-$"
  166.     s/(\s+)(['"])(-+)([^'"\-]*)\2(\s+|[,.])/"$1$2".&noremap("\$$3\$")."$4$2$5"/ge;
  167.     s/(\s+)(['"])([^'"\-]*)(-+)(\s*)\2(\s+|[,.])/"$1$2$3".&noremap("\$$4\$")."$5$2$6"/ge;
  168.     # (--|-)  =>  ($--$|$-$)
  169.     s/(\s+)\((-+)([=@%\$\+\\\|\w]*)(-*)([=@%\$\+\\\|\w]*)\)(\s+|[,.])/"$1\(".&noremap("\$$2\$")."$3".&noremap("\$$4\$")."$5\)$6"/ge;
  170.     # numeral -  =>  $-$
  171.     s/(\(|[0-9]+|\s+)-(\s*\(?\s*[0-9]+)/&noremap("$1\$-\$$2")/ge;
  172.     # -- in quotes  =>  two separate -
  173.     s/B<([^<>]*)--([^<>]*)>/&noremap("B<$1\{\\tt --\}$2>")/ge;
  174.     # backslash escapable characters except _.
  175.     s/([$backslash_escapables2])/&noremap("\\$1")/ge;
  176.     s/_/&noremap("\\underscore{}")/ge;        # a litle thicker than \_.
  177.     # quote TeX special characters |, ^, ~, \.
  178.     s/\|/&noremap("\$|\$")/ge;
  179.     s/\^/&noremap("\$\\hat{\\hspace{0.4em}}\$")/ge;
  180.     s/\~/&noremap("\$\\tilde{\\hspace{0.4em}}\$")/ge;
  181.     s/\\/&noremap("\$\\backslash{}\$")/ge;
  182.     # quote [ and ] to be used in \item[]
  183.     s/([\[\]])/&noremap("{\\tt $1}")/ge;
  184.     # characters need to be treated differently in TeX
  185.     # keep * if an item heading
  186.     s/^(=item[ \t]+)[*]((.|\n)*)/"$1" . &noremap("*") . "$2"/ge;
  187.     s/[*]/&noremap("\$\\ast\$")/ge;    # other *
  188.     # hide other pod quotes.
  189.     s/([ABD-Z]<[^<>]*>)/&noremap($1)/ge;
  190.     # escape < and > as math strings,
  191.     # now that we are done with hiding pod <> quotes.
  192.     s/</&noremap("\$<\$")/ge;
  193.     s/>/&noremap("\$>\$")/ge;
  194.     # put it back so we get the <> processed again;
  195.     $_ = &clear_noremap($_);
  196.     # Expand pod quotes recursively:
  197.     # (1) type face directives [BIFS]<[^<>]*> to appropriate TeX commands,
  198.     # (2) L<[^<>]*> to reference strings,
  199.     # (3) C<[^<>]*> to TeX literal quotes,
  200.     # (4) HTML quotes E<> inside of C<> quotes.
  201.     # Hide E<> again since they can be included in C<>.
  202.     s/(E<[^<>]+>)/noremap($1)/ge;
  203.     $maxnest = 10;
  204.     while ($maxnest-- && /[A-Z]</) {
  205.     # bold and italic quotes
  206.     s/B<([^<>]*)>/"{\\bf $1}"/eg;
  207.     s#I<([^<>]*)>#"{\\em $1\\/}"#eg;
  208.     # files and filelike refs in italics
  209.     s#F<([^<>]*)>#"{\\em $1\\/}"#eg;
  210.     # no break quote -- usually we want C<> for this
  211.     s/S<([^<>]*)>/&nobreak($1)/eg;
  212.     # LREF: a manpage(3f)
  213.     s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))?>:the {\\em $1\\/}$2 manpage:g;
  214.     # LREF: an =item on another manpage
  215.         L<([^/]+)/([:\w]+(\(\))?)>
  216.     } {the C<$2> entry in the I<$1> manpage}gx;
  217.     # LREF: an =item on this manpage
  218.        ((?:L</([:\w]+(\(\))?)>
  219.         (,?\s+(and\s+)?)?)+)
  220.     } { &internal_lrefs($1) }gex;
  221.     # LREF: a =head2 (head1?), maybe on a manpage, maybe right here
  222.     # the "func" can disambiguate
  223.         L<(?:([a-zA-Z]\S+?) /)?"?(.*?)"?>
  224.         do {
  225.         $1     # if no $1, assume it means on this page.
  226.             ?  "the section on I<$2> in the I<$1> manpage"
  227.             :  "the section on I<$2>"
  228.         } 
  229.     }gex;
  230.     s/Z<>/\\&/g;        # the "don't format me" thing
  231.     # comes last because not subject to reprocessing
  232.         C<([^<>]*)>
  233.         do {
  234.         ($str = $1) =~ tr/\200-\377/\000-\177/; #normalize hidden stuff
  235.         # expand HTML escapes if any;
  236.         # WARNING: if HTML escapes other than E<amp>,E<lt>,E<gt>,
  237.         # E<quot> are in C<>, they will not be printed correctly.
  238.         $str = &expand_HTML_escapes($str);
  239.         $strverb = &alltt($str);    # Tex verbatim escape of a string.
  240.         &noremap("$strverb");
  241.         }
  242.     }gex;
  243. #    if ( /C<([^<>]*)/ ) {
  244. #        $str = $1;
  245. #        if ($str !~ /\|/) {        # if includes |
  246. #        s/C<([^<>]*)>/&noremap("\\verb|$str|")/eg;
  247. #        } else {
  248. #        print STDERR "found \| in C<.*> at paragraph $.\n";
  249. #        # find a character not contained in $str to use it as a
  250. #        # separator of the \verb
  251. #        ($chars = $str) =~ s/(\W)/\\$1/g;
  252. #        ## ($chars = $str) =~ s/([\$<>,\|"'\-^{}()*+?\\])/\\$1/g;
  253. #        @fence = grep(!/[ $chars]/,@tex_verb_fences);
  254. #        s/C<([^<>]*)>/&noremap("\\verb$fence[0]$str$fence[0]")/eg;
  255. #        }
  256.     }
  257.     # process each pod command
  258.     if (s/^=//) {                # if a command
  259.     s/\n/ /g;
  260.     ($cmd, $rest) = split(' ', $_, 2);
  261.     $rest =~ s/^\s*//;
  262.     $rest =~ s/\s*$//;
  263.     if (defined $rest) {
  264.         &escapes;
  265.     $rest = &clear_noremap($rest);
  266.     $rest = &expand_HTML_escapes($rest);
  267.     if ($cmd eq 'cut') {
  268.         $cutting = 1;
  269.         $lastcmd = 'cut';
  270.     elsif ($cmd eq 'head1') {    # heading type 1
  271.         $rest =~ s/^\s*//; $rest =~ s/\s*$//;
  272.         print LATEX  "\n\\subsection*{$rest}";
  273.         # put index entry
  274.         ($index = $rest) =~ s/^(An?\s+|The\s+)//i;    # remove 'A' and 'The'
  275.         # index only those heads not matching the frequent patterns.
  276.         foreach $pat (@head1_freq_patterns) {
  277.         if ($index =~ /^$pat/) {
  278.             goto freqpatt;
  279.         }
  280.         print LATEX  "%\n\\index{$index}\n" if ($index);
  281.       freqpatt:
  282.         $lastcmd = 'head1';
  283.     elsif ($cmd eq 'head2') {    # heading type 2
  284.         $rest =~ s/^\s*//; $rest =~ s/\s*$//;
  285.         print LATEX  "\n\\subsubsection*{$rest}";
  286.         # put index entry
  287.         ($index = $rest) =~ s/^(An?\s+|The\s+)//i;    # remove 'A' and 'The'
  288.         $index =~ s/^Example\s*[1-9][0-9]*\s*:\s*//; # remove 'Example :'
  289.         print LATEX  "%\n\\index{$index}\n"  if ($index);
  290.         $lastcmd = 'head2';
  291.     elsif ($cmd eq 'over') {    # 1 level within a listing environment
  292.         push(@indent,$indent);
  293.         $indent = $rest + 0;
  294.         $lastcmd = 'over';
  295.     elsif ($cmd eq 'back') {    # 1 level out of a listing environment
  296.         $indent = pop(@indent);
  297.         warn "Unmatched =back\n" unless defined $indent;
  298.         $listingcmd = pop(@listingcmd);
  299.         print LATEX  "\n\\end{$listingcmd}\n"  if ($listingcmd);
  300.         $lastcmd = 'back';
  301.     elsif ($cmd eq 'item') {    # an item paragraph starts
  302.         if ($lastcmd eq 'over') {    # if we have just entered listing env
  303.         # see what type of list environment we are in.
  304.         if ($rest =~ /^[0-9]\.?/) {    # if numeral heading
  305.             $listingcmd = 'enumerate';
  306.         } elsif ($rest =~ /^\*\s*/) {    # if * heading
  307.             $listingcmd = 'itemize';
  308.         } elsif ($rest =~ /^[^*]/) {    # if other headings
  309.             $listingcmd = 'description';
  310.         } else {
  311.             warn "unknown list type for item $rest";
  312.         print LATEX  "\n\\begin{$listingcmd}\n";
  313.         push(@listingcmd,$listingcmd);
  314.         } elsif ($lastcmd ne 'item') {
  315.         warn "Illegal '=item' command without preceding 'over':";
  316.         warn "=item $bareitem";
  317.         }
  318.         if ($listingcmd eq 'enumerate') {
  319.         $rest =~ s/^[0-9]+\.?\s*//;    # remove numeral heading
  320.         print LATEX  "\n\\item";
  321.         print LATEX  "{\\bf $rest}" if $rest;
  322.         } elsif ($listingcmd eq 'itemize') {
  323.         $rest =~ s/^\*\s*//;        # remove * heading
  324.         print LATEX  "\n\\item";
  325.         print LATEX  "{\\bf $rest}" if $rest;
  326.         } else {                # description item
  327.         print LATEX  "\n\\item[$rest]";
  328.         }
  329.         $lastcmd = 'item';
  330.         $rightafter_item = 'yes';
  331.         # check if the item heading is short or long.
  332.         ($itemhead = $rest) =~ s/{\\bf (\S*)}/$1/g;
  333.         if (length($itemhead) < 4) {
  334.         $itemshort = "yes";
  335.         } else {
  336.         $itemshort = "no";
  337.         }
  338.         # write index entry
  339.         if ($pod =~ "perldiag") {            # skip 'perldiag.pod'
  340.         goto noindex;
  341.         }
  342.         # strip out the item of pod quotes and get a plain text entry
  343.         $bareitem =~ s/\n/ /g;            # remove newlines
  344.         $bareitem =~ s/\s*$//;            # remove trailing space
  345.         $bareitem =~ s/[A-Z]<([^<>]*)>/$1/g;    # remove <> quotes
  346.         ($index = $bareitem) =~ s/^\*\s+//;        # remove leading '*'
  347.         $index =~ s/^(An?\s+|The\s+)//i;        # remove 'A' and 'The'
  348.         $index =~ s/^\s*[1-9][0-9]*\s*[.]\s*$//; # remove numeral only
  349.         $index =~ s/^\s*\w\s*$//;            # remove 1 char only's
  350.         # quote ", @ and ! with " to be used in makeindex.
  351.         $index =~ s/"/""/g;                # quote "
  352.         $index =~ s/@/"@/g;                # quote @
  353.         $index =~ s/!/"!/g;                # quote !
  354.         ($rest2=$rest) =~ s/^\*\s+//;    # remove *
  355.         $rest2 =~ s/"/""/g;                # quote "
  356.         $rest2 =~ s/@/"@/g;                # quote @
  357.         $rest2 =~ s/!/"!/g;                # quote !
  358.         if ($pod =~ "(perlfunc|perlvar)") {    # when doc is perlfunc,perlvar
  359.         # take only the 1st word of item heading
  360.         $index =~ s/^([^{}\s]*)({.*})?([^{}\s]*)\s+.*/\1\2\3/;
  361.         $rest2 =~ s/^([^{}\s]*)({.*})?([^{}\s]*)\s+.*/\1\2\3/;
  362.         }
  363.         if ($index =~ /[A-Za-z\$@%]/) {
  364.             #  write  \index{plain_text_entry@TeX_string_entry}
  365.         print LATEX  "%\n\\index{$index\@$rest2}%\n";
  366.         }
  367.       noindex:
  368.         ;
  369.     elsif ($cmd eq 'pod') {
  370.         ;    # recognise the pod directive, as no op (hs)
  371.      elsif ($cmd eq 'pod') {
  372.          ;    # recognise the pod directive, as no op (hs)
  373.     else {
  374.         warn "Unrecognized directive: $cmd\n";
  375.     }
  376.     else {                    # if not command
  377.     &escapes;
  378.     $_ = &clear_noremap($_);
  379.     $_ = &expand_HTML_escapes($_);
  380.     # if the present paragraphs follows an =item declaration,
  381.     # put a line break.
  382.     if ($lastcmd eq 'item' &&
  383.         $rightafter_item eq 'yes' && $itemshort eq "no") {
  384.         print LATEX  "\\hfil\\\\";
  385.         $rightafter_item = 'no';
  386.     print LATEX  "\n",$_;
  387.     }
  388. print LATEX  "\n";
  389. close(POD);
  390. close(LATEX);
  391. #########################################################################
  392. sub do_hdr {
  393.     print LATEX "% LaTeX document produced by pod2latex from \"$pod.pod\".\n";
  394.     print LATEX "% The followings need be defined in the preamble of this document:\n";
  395.     print LATEX "%\\def\\C++{{\\rm C\\kern-.05em\\raise.3ex\\hbox{\\footnotesize ++}}}\n";
  396.     print LATEX "%\\def\\underscore{\\leavevmode\\kern.04em\\vbox{\\hrule width 0.4em height 0.3pt}}\n";
  397.     print LATEX "%\\setlength{\\parindent}{0pt}\n";
  398.     print LATEX "\n";
  399.     $podq = &escape_tex_specials("\U$pod\E");
  400.     print LATEX "\\section{$podq}%\n";
  401.     print LATEX "\\index{$podq}";
  402.     print LATEX "\n";
  403. sub nobreak {
  404.     my $string = shift;
  405.     $string =~ s/ +/~/g;        # TeX no line break
  406.     $string;
  407. sub noremap {
  408.     local($thing_to_hide) = shift;
  409.     $thing_to_hide =~ tr/\000-\177/\200-\377/;
  410.     return $thing_to_hide;
  411. sub init_noremap {
  412.     # escape high bit characters in input stream
  413.     s/([\200-\377])/"E<".ord($1).">"/ge;
  414. sub clear_noremap {
  415.     local($tmp) = shift;
  416.     $tmp =~ tr/\200-\377/\000-\177/;
  417.     return $tmp;
  418. sub expand_HTML_escapes {
  419.     local($s) = $_[0];
  420.     $s =~ s { E<((\d+)|([A-Za-z]+))> }
  421.     {
  422.     do {
  423.         defined($2) 
  424.         ? do { chr($2) }
  425.         exists $HTML_Escapes{$3}
  426.         ? do { $HTML_Escapes{$3} }
  427.         : do {
  428.         warn "Unknown escape: $& in $_";
  429.         "E<$1>";
  430.         }
  431.     }egx;
  432.     return $s;
  433. sub escapes {
  434.     # make C++ into \C++, which is to be defined as
  435.     # \def\C++{{\rm C\kern-.05em\raise.3ex\hbox{\footnotesize ++}}}
  436.     s/\bC\+\+/\\C++{}/g;
  437. # Translate a string into a TeX \tt string to obtain a verbatim print out.
  438. # TeX special characters are escaped by \.
  439. # This can be used inside of LaTeX command arguments.
  440. # We don't use LaTeX \verb since it doesn't work inside of command arguments.
  441. sub alltt {
  442.     local($str) = shift;
  443.     # other chars than #,\,$,%,&,{,},_,\,^,~ ([ and ] included).
  444.     $str =~ s/([^${backslash_escapables}\\\^\~]+)/&noremap("$&")/eg;
  445.     # chars #,\,$,%,&,{,}  =>  \# , ...
  446.     $str =~ s/([$backslash_escapables2])/&noremap("\\$&")/eg;
  447.     # chars _,\,^,~  =>  \char`\_ , ...
  448.     $str =~ s/_/&noremap("\\char`\\_")/eg;
  449.     $str =~ s/\\/&noremap("\\char`\\\\")/ge;
  450.     $str =~ s/\^/\\char`\\^/g;
  451.     $str =~ s/\~/\\char`\\~/g;
  452.     $str =~ tr/\200-\377/\000-\177/;        # put back
  453.     $str = "{\\tt ".$str."}";            # make it a \tt string
  454.     return $str;
  455. sub escape_tex_specials {
  456.     local($str) = shift;
  457.     # other chars than #,\,$,%,&,{,},  _,\,^,~ ([ and ] included).
  458.     # backslash escapable characters #,\,$,%,&,{,} except _.
  459.     $str =~ s/([$backslash_escapables2])/&noremap("\\$1")/ge;
  460.     $str =~ s/_/&noremap("\\underscore{}")/ge;    # \_ is too thin.
  461.     # quote TeX special characters |, ^, ~, \.
  462.     $str =~ s/\|/&noremap("\$|\$")/ge;
  463.     $str =~ s/\^/&noremap("\$\\hat{\\hspace{0.4em}}\$")/ge;
  464.     $str =~ s/\~/&noremap("\$\\tilde{\\hspace{0.4em}}\$")/ge;
  465.     $str =~ s/\\/&noremap("\$\\backslash{}\$")/ge;
  466.     # characters need to be treated differently in TeX
  467.     # *
  468.     $str =~ s/[*]/&noremap("\$\\ast\$")/ge;
  469.     # escape < and > as math string,
  470.     $str =~ s/</&noremap("\$<\$")/ge;
  471.     $str =~ s/>/&noremap("\$>\$")/ge;
  472.     $str =~ tr/\200-\377/\000-\177/;        # put back
  473.     return $str;
  474. sub internal_lrefs {
  475.     local($_) = shift;
  476.     s{L</([^>]+)>}{$1}g;
  477.     my(@items) = split( /(?:,?\s+(?:and\s+)?)/ );
  478.     my $retstr = "the ";
  479.     my $i;
  480.     for ($i = 0; $i <= $#items; $i++) {
  481.     $retstr .= "C<$items[$i]>";
  482.     $retstr .= ", " if @items > 2 && $i != $#items;
  483.     $retstr .= " and " if $i+2 == @items;
  484.     }
  485.     $retstr .= " entr" . ( @items > 1  ? "ies" : "y" )
  486.         .  " elsewhere in this document";
  487.     return $retstr;
  488. # map of HTML escapes to TeX escapes.
  489. BEGIN {
  490. %HTML_Escapes = (
  491.     'amp'    =>    '&',    #   ampersand
  492.     'lt'    =>    '<',    #   left chevron, less-than
  493.     'gt'    =>    '>',    #   right chevron, greater-than
  494.     'quot'    =>    '"',    #   double quote
  495.     "Aacute"    =>    "\\'{A}",    #   capital A, acute accent
  496.     "aacute"    =>    "\\'{a}",    #   small a, acute accent
  497.     "Acirc"    =>    "\\^{A}",    #   capital A, circumflex accent
  498.     "acirc"    =>    "\\^{a}",    #   small a, circumflex accent
  499.     "AElig"    =>    '\\AE',        #   capital AE diphthong (ligature)
  500.     "aelig"    =>    '\\ae',        #   small ae diphthong (ligature)
  501.     "Agrave"    =>    "\\`{A}",    #   capital A, grave accent
  502.     "agrave"    =>    "\\`{a}",    #   small a, grave accent
  503.     "Aring"    =>    '\\u{A}',    #   capital A, ring
  504.     "aring"    =>    '\\u{a}',    #   small a, ring
  505.     "Atilde"    =>    '\\~{A}',    #   capital A, tilde
  506.     "atilde"    =>    '\\~{a}',    #   small a, tilde
  507.     "Auml"    =>    '\\"{A}',    #   capital A, dieresis or umlaut mark
  508.     "auml"    =>    '\\"{a}',    #   small a, dieresis or umlaut mark
  509.     "Ccedil"    =>    '\\c{C}',    #   capital C, cedilla
  510.     "ccedil"    =>    '\\c{c}',    #   small c, cedilla
  511.     "Eacute"    =>    "\\'{E}",    #   capital E, acute accent
  512.     "eacute"    =>    "\\'{e}",    #   small e, acute accent
  513.     "Ecirc"    =>    "\\^{E}",    #   capital E, circumflex accent
  514.     "ecirc"    =>    "\\^{e}",    #   small e, circumflex accent
  515.     "Egrave"    =>    "\\`{E}",    #   capital E, grave accent
  516.     "egrave"    =>    "\\`{e}",    #   small e, grave accent
  517.     "ETH"    =>    '\\OE',        #   capital Eth, Icelandic
  518.     "eth"    =>    '\\oe',        #   small eth, Icelandic
  519.     "Euml"    =>    '\\"{E}',    #   capital E, dieresis or umlaut mark
  520.     "euml"    =>    '\\"{e}',    #   small e, dieresis or umlaut mark
  521.     "Iacute"    =>    "\\'{I}",    #   capital I, acute accent
  522.     "iacute"    =>    "\\'{i}",    #   small i, acute accent
  523.     "Icirc"    =>    "\\^{I}",    #   capital I, circumflex accent
  524.     "icirc"    =>    "\\^{i}",    #   small i, circumflex accent
  525.     "Igrave"    =>    "\\`{I}",    #   capital I, grave accent
  526.     "igrave"    =>    "\\`{i}",    #   small i, grave accent
  527.     "Iuml"    =>    '\\"{I}',    #   capital I, dieresis or umlaut mark
  528.     "iuml"    =>    '\\"{i}',    #   small i, dieresis or umlaut mark
  529.     "Ntilde"    =>    '\\~{N}',    #   capital N, tilde
  530.     "ntilde"    =>    '\\~{n}',    #   small n, tilde
  531.     "Oacute"    =>    "\\'{O}",    #   capital O, acute accent
  532.     "oacute"    =>    "\\'{o}",    #   small o, acute accent
  533.     "Ocirc"    =>    "\\^{O}",    #   capital O, circumflex accent
  534.     "ocirc"    =>    "\\^{o}",    #   small o, circumflex accent
  535.     "Ograve"    =>    "\\`{O}",    #   capital O, grave accent
  536.     "ograve"    =>    "\\`{o}",    #   small o, grave accent
  537.     "Oslash"    =>    "\\O",        #   capital O, slash
  538.     "oslash"    =>    "\\o",        #   small o, slash
  539.     "Otilde"    =>    "\\~{O}",    #   capital O, tilde
  540.     "otilde"    =>    "\\~{o}",    #   small o, tilde
  541.     "Ouml"    =>    '\\"{O}',    #   capital O, dieresis or umlaut mark
  542.     "ouml"    =>    '\\"{o}',    #   small o, dieresis or umlaut mark
  543.     "szlig"    =>    '\\ss{}',    #   small sharp s, German (sz ligature)
  544.     "THORN"    =>    '\\L',        #   capital THORN, Icelandic
  545.     "thorn"    =>    '\\l',,        #   small thorn, Icelandic
  546.     "Uacute"    =>    "\\'{U}",    #   capital U, acute accent
  547.     "uacute"    =>    "\\'{u}",    #   small u, acute accent
  548.     "Ucirc"    =>    "\\^{U}",    #   capital U, circumflex accent
  549.     "ucirc"    =>    "\\^{u}",    #   small u, circumflex accent
  550.     "Ugrave"    =>    "\\`{U}",    #   capital U, grave accent
  551.     "ugrave"    =>    "\\`{u}",    #   small u, grave accent
  552.     "Uuml"    =>    '\\"{U}',    #   capital U, dieresis or umlaut mark
  553.     "uuml"    =>    '\\"{u}',    #   small u, dieresis or umlaut mark
  554.     "Yacute"    =>    "\\'{Y}",    #   capital Y, acute accent
  555.     "yacute"    =>    "\\'{y}",    #   small y, acute accent
  556.     "yuml"    =>    '\\"{y}',    #   small y, dieresis or umlaut mark
  557.